home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Camelot / Camelot 134 (1991-10)(Swedish User Group of Amiga)(SE)(PD)[WB].zip / Camelot 134 (1991-10)(Swedish User Group of Amiga)(SE)(PD)[WB].adf / RxSlides / open_pic.c < prev    next >
C/C++ Source or Header  |  1990-03-08  |  3KB  |  117 lines

  1. /* :bk=0 */
  2.  
  3. /************************************************************************/
  4. /*                                    */
  5. /*                        Open_pic.c        */
  6. /*                                    */
  7. /* Author: Lee M. Robertson, mods by Dean Bandes            */
  8. /*    Based on an article in the Nov '86 Doctor Dobb's Journal     */
  9. /*    by Mike Morton                            */
  10. /*                                    */
  11. /************************************************************************/
  12.  
  13. #include "exec/types.h"
  14. #include "exec/memory.h"
  15. #include "graphics/gfx.h"
  16. #include "fcntl.h"
  17. #include "RxSlides.h"
  18. #include "RxSliPic.h"
  19.  
  20. typedef unsigned long ID;
  21.  
  22. struct Chunk
  23. {
  24.     ID    ckID;
  25.     long    ckSize;
  26. };
  27.  
  28. #define MakeID(a,b,c,d) ( (long)(a)<<24 | (long)(b)<<16 | (long)(c)<<8 | (d) )
  29. #define FORM MakeID('F','O','R','M')
  30. #define ILBM MakeID('I','L','B','M')
  31. #define LIST MakeID('L','I','S','T')
  32. #define BODY MakeID('B','O','D','Y')
  33. #define BMHD MakeID('B','M','H','D')
  34. #define CMAP MakeID('C','M','A','P')
  35.  
  36. #define CSIZE sizeof(chunk)
  37. #define BMHDSIZE sizeof( struct BitMapHeader)
  38.  
  39. /*---------------------------------------------------------------------------
  40.  * open_pic() -- open a picture file and initialize the picture data struct
  41.  *-------------------------------------------------------------------------*/
  42. int   open_pic( filename, pic )
  43. char *filename;                /* the picture file name */
  44. register struct picture *pic;        /* the picture data */
  45. {
  46.     register int fd;        /* the picture file descriptor */
  47.     struct Chunk chunk;        /* temp for read data */
  48.     register int cmsize;
  49.  
  50.     fd = open(filename, O_RDONLY);    /* open the file */
  51.     if( fd == -1)
  52.         return (STS_NO_PIC_OPEN);
  53.  
  54.     if( read(fd, (char *) &chunk, CSIZE) != CSIZE )
  55.         return (STS_READ_ERROR);
  56.  
  57.     if( chunk.ckID != FORM)
  58.         return (STS_NOT_PICTURE);
  59.  
  60.     /* check for ILBM file */
  61.     if( read(fd, (char *) &chunk, sizeof(long) ) != sizeof(long) )
  62.         return (STS_READ_ERROR);
  63.     if( chunk.ckID != ILBM )
  64.         return (STS_NOT_ILBM);
  65.  
  66.     if( read(fd, (char *) &chunk, CSIZE) != CSIZE )
  67.         return (STS_READ_ERROR);
  68.     
  69.     if( chunk.ckID != BMHD)
  70.         return (STS_BAD_BMHD);
  71.  
  72.     if( chunk.ckSize != BMHDSIZE )
  73.         return (STS_BAD_BMHDSIZE);
  74.  
  75.     /* read the BMHD data */
  76.     if( read( fd, (char *) &pic->bmhd, BMHDSIZE ) != BMHDSIZE )
  77.         return (STS_READ_ERROR);
  78.  
  79.     pic->numcolors = 1;            /* set default color map */
  80.  
  81.     /* read chunks untill body is found */
  82.     while( read(fd, (char *) &chunk, CSIZE) == CSIZE )
  83.     {
  84.  
  85.         if( chunk.ckID == CMAP)
  86.         {
  87.             /* read the color map */
  88.  
  89.             /* get size of the color map */
  90.             if( chunk.ckSize > CMSIZE )
  91.                 cmsize = CMSIZE;
  92.             else
  93.                 cmsize = chunk.ckSize;
  94.  
  95.             if( read(fd, &pic->cmap[0], cmsize ) != cmsize )
  96.                 return (STS_READ_ERROR);
  97.             pic->numcolors = cmsize / 3;        /* save # of colors in picture */
  98.  
  99.             chunk.ckSize++;
  100.             chunk.ckSize &= ~1;
  101.  
  102.             if( chunk.ckSize -= cmsize )
  103.                 lseek( fd, chunk.ckSize, 1 );    /* skip rest of colormap */
  104.         }
  105.  
  106.         if( chunk.ckID == BODY )
  107.         {
  108.             pic->bodysize = chunk.ckSize;
  109.             pic->fd = fd;
  110.             return (STS_OK);    /* no errs */
  111.         }
  112.  
  113.         /* else ignore this chunks data */
  114.         lseek(fd,chunk.ckSize,1);
  115.     }
  116. }
  117.